﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Collections;
using System.IO;

namespace VPC.UI
{
    public partial class OcrFormNext : Form
    {
        public OcrFormNext()
        {
            InitializeComponent();
            string doc = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToString();
            doc += "\\test.bmp";
            pictureBox1.Image = Image.FromFile(doc);
            Modi(doc);
        }

        public void Modi(string doc)
        {
            Console.WriteLine(doc);
            //IEnumerator files = Directory.GetFiles(doc).GetEnumerator();
            //while (files.MoveNext())
            //{
                //get file extension 
            string fileExtension = Path.GetExtension(doc);

                //get file name without extenstion 
            string fileName = Convert.ToString(doc).Replace(fileExtension, string.Empty);

                //Check for JPG File Format 
                if (fileExtension == ".bmp" || fileExtension == ".BMP") // or // ImageFormat.Jpeg.ToString()
                {
                    try
                    {
                        //OCR Operations ... 
                        MODI.Document md = new MODI.Document();
                        md.Create(doc);
                        //md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
                        MODI.Image image = (MODI.Image)md.Images[0];

                        //create text file with the same Image file name 
                        FileStream createFile = new FileStream(fileName + ".txt", FileMode.CreateNew);

                        //save the image text in the text file 
                        StreamWriter writeFile = new StreamWriter(createFile);
                        writeFile.Write(image.Layout.Text);
                        writeFile.Close();
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("This Image hasn't a text or has a problem",
                        "OCR Notifications",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Hide();
                    }
                }
            //} 

            //MODI.Document _MODIDocument = new MODI.Document();
            ////Document _MODIDocument = new Document();
            //_MODIDocument.Create(doc);

            //_MODIDocument.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
            //MODI.Image img = (MODI.Image)_MODIDocument.Images[0];
            //MODI.Layout layout = img.Layout;

            //StringBuilder sb = new StringBuilder();
            //for (int i = 0; i < layout.Words.Count; i++)
            //{
            //    MODI.Word word = (MODI.Word)layout.Words[i];
            //    sb.Append(word.Text);
            //    sb.Append(" ");
            //}

            //if (sb.Length > 1)
            //    sb.Length--;

            //listBox_word.Items.Clear();

            //string[] words = sb.ToString().Split(' ');
            //foreach (string word in words)
            //{
            //    listBox_word.Items.Add(word); // <-- Add these
            //}
        }
    }
}
